package org.onejava.core.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.onejava.core.tools.BaseIocTool;


public class MContext {
	public MContext(){
		
	}
	private HttpServletRequest request;
	private HttpServletResponse response;
	//class路径
	private String classPath;
	//是否上传文件
	boolean isMultipart;
	//文件列表
	List<FileItem> files;
	//参数	
	private Map<String,Object> map;
	//返回结果
	private String result;
	//返回结果
	private Map<String,Object> resultMap;
	
	public Object getBean(String name,Class<?> className) throws Exception{
		return BaseIocTool.setBean((Map)getValue(name), className);
	}
	public Map getMap(String name){
		return (Map)getValue(name);
		
	}
	public void setReValue(String key,Object value){
		resultMap.put(key, value);
	}
	public List<Object> getList(String key){
		List<Object> list = new ArrayList<Object>();
		Object value = map.get(key);
		if(null==value){
			return list;
		}
		if(value.getClass().equals(String[].class)){
			String[] values=(String[]) value;
			for(int i =0;i<values.length;i++){
				list.add(values[i]);
			}
			return list;
			
		}if(value.getClass().equals(String.class)){
			list.add(value);
			return list;
		}
		return list;
	}
	public List<FileItem> getFiles() {
		return files;
	}
	public void setFiles() throws FileUploadException {
		boolean isMultipart= ServletFileUpload.isMultipartContent(this.request);
		if(isMultipart&&null==files){
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			this.files= upload.parseRequest(this.request);
		}
	}
	public Object getValue(String key){
		Object value = map.get(key);
		return value;
	}
	public Object getValue(String key,Class<?> classType) throws Exception{
		return BaseIocTool.changeType(classType, (String)map.get(key));		
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public MContext(HttpServletRequest request, HttpServletResponse response){
		if(request==null||response==null){
			
		}else{
			this.request=request;
			this.response=response;
		}
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	

	//public Object getValue(String key){		
		//return request.getAttribute("key");		
	//}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	
	public String getClassPath() {
		return classPath;
	}
	public void setClassPath(String classPath) {
		this.classPath = classPath;
	}
	public Map<String,Object> getResultMap() {
		return resultMap;
	}
	public void setResultMap(Map<String,Object> resultMap) {
		this.resultMap = resultMap;
	}
	public boolean isMultipart() {
		return isMultipart;
	}
	public void setMultipart(boolean isMultipart) {
		this.isMultipart = isMultipart;
	}
	
}
